﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HRMSys.Model;
using HRMSys.DAL;

namespace HRMSys.UI.SystemMgr
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class DepartmentEditUI : Window
    {
        public bool IsInsert { get; set; }
        public Guid EditingId { get; set; }

        public DepartmentEditUI()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsInsert == true)
            {
                cbIsEnable.IsChecked = true;
            }
            else 
            {
                DepartmentDAL dal = new DepartmentDAL();
                Department depart = dal.GetById(EditingId);
                txtDepartName.Text = depart.DepartName;
                cbIsEnable.IsChecked = depart.IsEnable;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string departName = txtDepartName.Text;
            if (departName.Length <= 0)
            {
                MessageBox.Show("请输入部门名称", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else 
            {
                if (this.IsInsert == true)  //新增
                {
                    Department depart = new Department();
                    depart.DepartName = txtDepartName.Text;
                    //depart.IsDeleted = rbEnable.IsChecked == true ? true : false;

                    DepartmentDAL dal = new DepartmentDAL();
                    int result = dal.Insert(depart);
                    if (result == 1)
                    {
                        MessageBox.Show("新增部门成功", "系统提示");
                        DialogResult = true;
                    }
                    else 
                    {
                        MessageBox.Show("新增部门失败", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
                else //编辑 
                {
                    if (txtDepartName.Text.Length <= 0) 
                    {
                        MessageBox.Show("请输入部门名称!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    
                    Department depart = new Department();
                    depart.Id = EditingId;
                    depart.DepartName = txtDepartName.Text;
                    depart.IsEnable = cbIsEnable.IsChecked == true;

                    DepartmentDAL dal = new DepartmentDAL();
                    int result = dal.Update(depart);
                    if (result != 1)
                    {
                        MessageBox.Show("修改失败", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    else 
                    {
                        MessageBox.Show("修改成功", "系统提示", MessageBoxButton.OK);
                        DialogResult = true;
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}
